using System;
using System.Web.UI.WebControls;
using Activa.CoolStorage;
using CodeStory.Core;
using CodeStory.Core.UI;
using CodeStory.Gallery.ObjectBusiness;
using CodeStory.Gallery.ObjectEntity;

namespace CodeStory.Gallery.Portlets
{
	[NavigationPortlet("CodeStory_Gallery_GalleryFoldersList")]
	public class GalleryFoldersList : Portlet
	{
		protected int _isgalleryowner = -1;
		protected global::System.Web.UI.WebControls.DataList dlFolders;
		protected global::CodeStory.WebControls.EditableLabel elblCreateFolder;
		private string createfoldertext = "Double click the lable to create an folder.";

		private bool IsGalleryOwner {
			get {
				if (_isgalleryowner == -1) {
					string userid = Request.QueryString["galleryid"];
					string cur = UserContext.Current.UserID.ToString();
					if (string.Equals(userid, cur, StringComparison.OrdinalIgnoreCase)) {
						_isgalleryowner = 1;
					} else {
						_isgalleryowner = 0;
					}
				}
				return _isgalleryowner > 0;
			}
		}

		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(GalleryFoldersList_Load);
			dlFolders.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(DataListFolders_ItemDataBound);
			dlFolders.ItemCommand += new DataListCommandEventHandler(DataListFolders_ItemCommand);
			elblCreateFolder.TextChanged += new EventHandler(CreateFolder_TextChanged);
			base.OnInit(e);
		}

		void CreateFolder_TextChanged(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			string text = elblCreateFolder.Text;
			if (string.IsNullOrEmpty(text)) return;
			GalleryFolderBusiness.CreateFolder(text,UserContext.Current.UserID);
			elblCreateFolder.Text = createfoldertext;
			BindGalleryFolders();
		}

		void DataListFolders_ItemCommand(object source, DataListCommandEventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			string folderid = e.CommandArgument.ToString();
			switch (e.CommandName.ToLower()) {
				case "deletefolder":
					GalleryFolderBusiness.DeleteGalleryFolder(folderid);
					break;
				case "togglepublic":
					GalleryFolderBusiness.ToggleFolderPublic(folderid);
					break;
			}
			BindGalleryFolders();
		}

		private void DataListFolders_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			WebControl ctrl = e.Item.FindControl("admin") as WebControl;
			if (ctrl == null) {
				return;
			}
			ctrl.Visible = IsGalleryOwner;
		}

		private void GalleryFoldersList_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (!IsPostBack && !EditMode) {
				elblCreateFolder.Text = createfoldertext;
				BindGalleryFolders();
			}
			if (!EditMode) {
				elblCreateFolder.Visible = IsGalleryOwner;
			}
		}

		private void BindGalleryFolders() {
			//hacks,this use the galleryid as the query string,not userid.
			string userid = Request.QueryString["galleryid"];
			if (string.IsNullOrEmpty(userid)) {
				return;
			}
			CSList<GalleryFolderObject> list = GalleryFolderBusiness.ListUserGalleryFolders(userid, UserContext.Current.UserID);
			dlFolders.DataSource = list;
			dlFolders.DataBind();
		}
	}
}